#user、stu_new
import flask
import json
import MySQL


server=flask.Flask(__name__)

@server.route("/table_data",methods=['post','get'])
def user():
    '''从数据库查询数据并通过接口返回'''
    table_names=['user','stu_new']
    table_name=flask.request.values.get("table_name")
    if table_name:
        if table_name in table_names:
            sql='select * from %s' % table_name
            result = MySQL.execute_sql(sql)
            data={"code":0,"msg":"成功","data":result}
            return  json.dumps(data,ensure_ascii=False)
        else:
            return json.dumps({"code":-1,"msg":"没有查询该表的权限"},ensure_ascii=False)
    return  json.dumps({"code":-1,"msg":"请传入table_name"},ensure_ascii=False)

#http://127.0.0.1:8899/table_data?table_name=user

server.run(port=8899,debug=True,host='0.0.0.0')
